﻿var map, dialog;
require([
    "esri/map", "esri/layers/FeatureLayer","esri/geometry/Extent",
    "esri/symbols/SimpleFillSymbol", "esri/symbols/SimpleLineSymbol",
    "esri/renderers/SimpleRenderer", "esri/graphic", "esri/lang",
    "esri/Color", "dojo/number", "dojo/dom-style","esri/geometry/Point", "esri/SpatialReference",
    "dijit/TooltipDialog", "dijit/popup","esri/renderers/UniqueValueRenderer", "dojo/domReady!"
], function(
    Map, FeatureLayer,Extent,
    SimpleFillSymbol, SimpleLineSymbol,
    SimpleRenderer, Graphic, esriLang,
    Color, number, domStyle,Point,spatialReference,
    TooltipDialog, dijitPopup
){
    map = new Map("map", {
        basemap: "streets",
        center: [120.155,30.16],
        zoom: 14,
        slider: false
    });

    var land = new FeatureLayer("http://192.168.1.157:8399/arcgis/rest/services/zhxtd/ground_plan/MapServer/2", {
        mode: FeatureLayer.MODE_SNAPSHOT,
        outFields: ["*"]
    });
    //southCarolinaCounties.setDefinitionExpression("STATE_NAME = 'South Carolina'");
    var symbol = new SimpleFillSymbol(
        SimpleFillSymbol.STYLE_SOLID,
        new SimpleLineSymbol(
            SimpleLineSymbol.STYLE_SOLID,
            new Color([100,149,237]),
            1
        ),
        new Color([95,158,16,0.85])
    );
    land.setRenderer(new SimpleRenderer(symbol));
    map.addLayer(land);

    map.infoWindow.resize(245,125);

    dialog = new TooltipDialog({
        id: "tooltipDialog",
        style: "position: absolute; width: 250px; font: normal normal normal 10pt Helvetica;z-index:100"
    });
    dialog.startup();

    var highlightSymbol = new SimpleFillSymbol(
        SimpleFillSymbol.STYLE_SOLID,
        new SimpleLineSymbol(
            SimpleLineSymbol.STYLE_SOLID,
            new Color([0,255,255]), 3
        ),
        new Color([125,125,125,0.35])
    );

    //close the dialog when the mouse leaves the highlight graphic
    //map.on("load", function(){
    //    map.graphics.enableMouseEvents();
    //    map.graphics.on("mouse-out", closeDialog);
    //
    //});

    //listen for when the onMouseOver event fires on the countiesGraphicsLayer
    //when fired, create a new graphic with the geometry from the event.graphic and add it to the maps graphics layer
    land.on("click", function(evt){
        map.graphics.clear();
        dijitPopup.close(dialog);
        var t = "<b>${ID}</b><hr><b>Di Lei: </b>${DL}<br>"
            + "<b>Type: </b>${type}<br>"
            + "<b>x: </b>${X}<br>"
            + "<b>y: </b>${Y}";

        var content = esriLang.substitute(evt.graphic.attributes,t);
        var highlightGraphic = new Graphic(evt.graphic.geometry,highlightSymbol);
        map.graphics.add(highlightGraphic);

        dialog.setContent(content);

        domStyle.set(dialog.domNode, "opacity", 0.85);

        map.centerAt(evt.mapPoint);
        var center=map.toScreen(map.extent.getCenter());
        dijitPopup.open({
            popup: dialog,
            x: center.x,
            y: center.y
        });
    });

    //function closeDialog() {
    //    map.graphics.clear();
    //    dijitPopup.close(dialog);
    //}

});